package com.zjdiepu.www.rest.api.base;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Id;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.core.ParentModel;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.commpent.mapper.other.OtherNoticeVoMapper;
import com.zjdiepu.www.ibiz.other.IOtherNoticeService;
import com.zjdiepu.www.model.other.dto.OtherNotice;
import com.zjdiepu.www.model.other.ext.OtherNoticeExt;
import com.zjdiepu.www.model.other.request.OtherNoticeRequest;
import com.zjdiepu.www.model.other.response.OtherNoticeResponse;

/**
 * 公告Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年06月09日 18时59分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "OtherNoticeController", produces="application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = {"公告"}, produces="application/json;charset=UTF-8")
public class OtherNoticeController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(OtherNoticeController.class);
	
    @Resource
    private IOtherNoticeService otherNoticeService;
    @Resource
    private OtherNoticeVoMapper otherNoticeVoMapper;
    @Resource
    private TokenUserHelper tokenUserHelper;
    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询公告<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询公告", response = OtherNotice.class, httpMethod="POST")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("getById")
    public ApiResponse getById(@Validated(Id.class) @RequestBody ParentModel parentModel){
//       tokenUserHelper.getUser();
       OtherNotice otherNotice = otherNoticeService.selectById(parentModel.getId());
       if(null == otherNotice) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(otherNotice);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询公告 <br/>
     * @param otherNoticeExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询公告",response = OtherNoticeResponse.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("list")
    public ApiResponse list(@Validated @RequestBody OtherNoticeRequest otherNoticeRequest){
    	tokenUserHelper.getUser();
    	OtherNoticeExt otherNoticeExt = new OtherNoticeExt();
    	otherNoticeExt.setBaseWhere();
    	//截止今日没有过期
    	otherNoticeExt.setDateValidStart(DateUtil.truncAndAddDays(new Date(), 0));
    	otherNoticeExt.setOrderSql("`CREATED` DESC"); //最新
    	otherNoticeExt.setPage(otherNoticeRequest.getPage());
    	otherNoticeExt.setRows(otherNoticeRequest.getRows());
        int records = otherNoticeService.countByExt(otherNoticeExt);
        
        PageVo page = getPage(otherNoticeExt);
        page.setRecords(records);
        if (records > 0) {
            List<OtherNotice> otherNotices = otherNoticeService.selectByExt(otherNoticeExt);
            List<OtherNoticeResponse> otherNoticeResponses = mapOtherNotices(otherNotices);
            page.setRows(otherNoticeResponses);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
//  ============================================辅助方法===========================================
    
  /**
   * 转换属性：展示
   * @param ywBusinesss
   * @return
   */
  private List<OtherNoticeResponse> mapOtherNotices(List<OtherNotice> otherNotices){
	  if(CollectionUtils.isEmpty(otherNotices)) {
	  		return CollectionInitUtil.emptyList();
	  	}
	  	List<OtherNoticeResponse> otherNoticeResponses = CollectionInitUtil.emptyList();
	  	for(OtherNotice otherNotice : otherNotices) {
	  		OtherNoticeResponse otherNoticeResponse = mapOtherNotice(otherNotice);
	  		if(otherNoticeResponse != null) {
	  			otherNoticeResponses.add(otherNoticeResponse);
	  		}
	  	}
	  	return otherNoticeResponses;
  }
  
  /**
   * 转换属性：展示
   * @param ywBusinesss
   * @return
   */
  private OtherNoticeResponse mapOtherNotice(OtherNotice otherNotice){
  	if(otherNotice == null) {
  		return null;
  	}
  	try {
		OtherNoticeResponse otherNoticeResponse = new OtherNoticeResponse();
		BeanUtils.copyProperties(otherNoticeResponse, otherNotice);
		return otherNoticeResponse;
	} catch (IllegalAccessException | InvocationTargetException e) {
		logger.error("对象拷贝属性出错.", e);
		return null;
	}
  }
}
